/**
 * 
 */
package neptune.web.client.view.components.editor;

import neptune.web.client.model.topology.SerializableNode;
import neptune.web.client.model.topology.SerializableTopology;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 *
 */
public class RemoveNodeCommand implements Command {

	private SerializableTopology topology;
	private SerializableNode node;
	private TreeItem item;
	private final TopologyEditor editor;

	public RemoveNodeCommand(SerializableTopology topology,
			SerializableNode node, TreeItem item, TopologyEditor editor) {
		this.topology = topology;
		this.node = node;
		this.item = item;
		this.editor = editor;
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.Command#execute()
	 */
	public void execute() {
		topology.removeNode(node);
		item.remove();
		
		for (int i = 0; i < editor.getGraphPanel().getWidgetCount(); i++) {
			if(editor.getGraphPanel().getWidget(i) instanceof Label){
				Label nodeGraph = (Label) editor.getGraphPanel().getWidget(i);
				if(nodeGraph.getText().equals(node.getId())){
					nodeGraph.removeFromParent();
					editor.getGraphPanel().removeConnectionsWithEndPoint(nodeGraph);
					editor.updateGraphics();
					break;
				}
			}
		}
	}

}
